package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 从mysql中查询数据同步到redis数据库中
 * redis数据预热
 */
@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent>{

    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyStatisVOService strategyStatisVOService;

    //spring容器启动，并且初始化之后 马上执行操作
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("---------vo数据的初始化 开始------");
        //mysql  -- redis -- vo
        List<StrategyDetail> list = strategyDetailService.list();
        for (StrategyDetail strategyDetail : list) {

            //如果vo已经存在就跳过不需要初始化，以免数据被覆盖
            if (strategyStatisVOService.isStrategyVOExist(strategyDetail.getId())){
                continue;//跳过
            }

            StrategyStatisVO vo = new StrategyStatisVO();

            BeanUtils.copyProperties(strategyDetail, vo);//同属姓名可以拷贝
            //目的地,id,name,攻略明细id
            vo.setDestId(strategyDetail.getDest().getId());
            vo.setDestName(strategyDetail.getDest().getName());
            vo.setStrategyId(strategyDetail.getId());
            strategyStatisVOService.setStrategyStaticVO(vo);
        }

        System.out.println("---------vo数据的初始化 结束------");

        System.out.println("---------zset数据的初始化 开始------");
        //热门初始化
        for (StrategyDetail strategyDetail : list) {
            String voId = RedisKeys.STRATEGY_STATIS_VO.join(strategyDetail.getId().toString());
            String zsetKey = RedisKeys.STRATEGY_HOT_ZSET.getPrefix();
            //判断某个voId是否存在指定的zset中
            if (strategyStatisVOService.isVoidExsitZset(voId, zsetKey)){
                continue;
            }
            int score = strategyDetail.getViewnum() + strategyDetail.getReplynum();
            strategyStatisVOService.addScoreHot(strategyDetail.getId(), score);
        }

        //国外初始化
        for (StrategyDetail strategyDetail : list) {
            String voId = RedisKeys.STRATEGY_STATIS_VO.join(strategyDetail.getId().toString());
            String zsetKey = null;

            if (strategyDetail.isIsabroad()){
                //国内
                zsetKey = RedisKeys.STRATEGY_CHINA_ZSET.getPrefix();
            }else {
                //国外
                zsetKey = RedisKeys.STRATEGY_ABROAD_ZSET.getPrefix();
            }
            //判断某个voId是否存在指定的zset中
            if (strategyStatisVOService.isVoidExsitZset(voId, zsetKey)){
                continue;
            }
            //点赞加收藏
            int score = strategyDetail.getThumbsupnum() + strategyDetail.getFavornum();
            strategyStatisVOService.addScoreAbroadAndChina(strategyDetail.getId(), score);
        }
        System.out.println("---------zset数据的初始化 结束------");
    }
}
